package dao

import (
	"context"
	"encoding/json"
	"github.com/olivere/elastic/v7"
)

type Searcher[T any] struct {
	client    *elastic.Client
	indexName []string
	query     elastic.Query
}

func NewSearcher[T any](client *elastic.Client, indexName ...string) *Searcher[T] {
	return &Searcher[T]{client: client, indexName: indexName}
}

func (s *Searcher[T]) Query(query elastic.Query) *Searcher[T] {
	s.query = query
	return s
}
func (s *Searcher[T]) Do(ctx context.Context) ([]T, error) {
	resp, err := s.client.Search(s.indexName...).Query(s.query).Do(ctx)
	if err != nil {
		return nil, err
	}
	res := make([]T, 0, resp.Hits.TotalHits.Value)
	for _, hit := range resp.Hits.Hits {
		var t T
		err = json.Unmarshal(hit.Source, &t)
		if err != nil {
			return nil, err
		}
		res = append(res, t)
	}
	return res, nil
}

type ArticleReq struct {
	TagArtids     []int64
	LikeArtids    []int64
	CollectArtids []int64
}
